{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 8846 entries, 0 to 8845\n",
      "Columns: 111 entries, Unnamed: 0 to c_other\n",
      "dtypes: float64(2), int64(104), object(5)\n",
      "memory usage: 7.5+ MB\n",
      "None\n",
      "   Unnamed: 0    event_id    user_id                start_time city state  \\\n",
      "0           2  3928440935  517514445  2012-11-05T00:00:00.001Z  NaN   NaN   \n",
      "\n",
      "   zip country  lat  lng   ...     c_92  c_93  c_94  c_95  c_96  c_97  c_98  \\\n",
      "0  NaN     NaN  NaN  NaN   ...        0     0     0     0     0     0     0   \n",
      "\n",
      "   c_99  c_100  c_other  \n",
      "0     0      0       12  \n",
      "\n",
      "[1 rows x 111 columns]\n",
      "(8846, 102)\n",
      "(7076, 102)\n",
      "(1770, 102)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.cluster import MiniBatchKMeans\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import metrics\n",
    "\n",
    "from sklearn.decomposition import PCA\n",
    "import time\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "train = pd.read_csv('./train_events.csv')\n",
    "\n",
    "labels = [\"event_id\",\"user_id\",\"start_time\",\"city\",\"state\",\"zip\",\"country\",\"lat\",\"lng\"]\n",
    "# print(X_train_pca.shape)\n",
    "\n",
    "# n_trains = 1000\n",
    "# y_train = train.label.values[:n_trains]\n",
    "# X_train = train.drop(\"label\",axis=1).values[:n_trains]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 8846 entries, 0 to 8845\n",
      "Columns: 111 entries, Unnamed: 0 to c_other\n",
      "dtypes: float64(2), int64(104), object(5)\n",
      "memory usage: 7.5+ MB\n",
      "None\n",
      "   Unnamed: 0    event_id    user_id                start_time city state  \\\n",
      "0           2  3928440935  517514445  2012-11-05T00:00:00.001Z  NaN   NaN   \n",
      "\n",
      "   zip country  lat  lng   ...     c_92  c_93  c_94  c_95  c_96  c_97  c_98  \\\n",
      "0  NaN     NaN  NaN  NaN   ...        0     0     0     0     0     0     0   \n",
      "\n",
      "   c_99  c_100  c_other  \n",
      "0     0      0       12  \n",
      "\n",
      "[1 rows x 111 columns]\n",
      "(8846, 102)\n"
     ]
    }
   ],
   "source": [
    "# n_trains = 10000\n",
    "# y_train = train[\"event_id\"].values[:n_trains]\n",
    "# X_train = train.drop(labels,axis=1).values[:n_trains]\n",
    "\n",
    "y_train = train[\"event_id\"]\n",
    "X_train = train.drop(labels,axis=1)\n",
    "\n",
    "print train.info()\n",
    "print train.head(1)\n",
    "print(X_train.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_train.shap\n",
      "(7076, 102)\n",
      "x_val.shape\n",
      "(1770, 102)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "X_train_part, X_val, y_train_part, y_val = train_test_split(X_train,y_train, train_size = 0.8,random_state = 0)\n",
    "print \"x_train.shap\"\n",
    "print(X_train_part.shape)\n",
    "print \"x_val.shape\"\n",
    "print(X_val.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K-means begin with clusters: 10\n",
      "CH_score: 0.555724903775, time elaps:1\n",
      "v_score: 0.468538637564\n",
      "K-means begin with clusters: 20\n",
      "CH_score: 0.548362503262, time elaps:1\n",
      "v_score: 0.569701891275\n",
      "K-means begin with clusters: 30\n",
      "CH_score: 0.538919586808, time elaps:1\n",
      "v_score: 0.620462258237\n",
      "K-means begin with clusters: 40\n",
      "CH_score: 0.545792919202, time elaps:1\n",
      "v_score: 0.655269537855\n",
      "K-means begin with clusters: 50\n",
      "CH_score: 0.538773826533, time elaps:1\n",
      "v_score: 0.68007782743\n",
      "K-means begin with clusters: 60\n",
      "CH_score: 0.540265721658, time elaps:1\n",
      "v_score: 0.700241996716\n",
      "K-means begin with clusters: 70\n",
      "CH_score: 0.542158761018, time elaps:1\n",
      "v_score: 0.714561135015\n",
      "K-means begin with clusters: 80\n",
      "CH_score: 0.541821054478, time elaps:2\n",
      "v_score: 0.729397235161\n",
      "K-means begin with clusters: 90\n",
      "CH_score: 0.542326027237, time elaps:2\n",
      "v_score: 0.742162938724\n",
      "K-means begin with clusters: 100\n",
      "CH_score: 0.547315241823, time elaps:2\n",
      "v_score: 0.750492958215\n",
      "K-means begin with clusters: 110\n",
      "CH_score: 0.544332109105, time elaps:2\n",
      "v_score: 0.759569176675\n",
      "K-means begin with clusters: 120\n",
      "CH_score: 0.542767014491, time elaps:2\n",
      "v_score: 0.768762560441\n",
      "K-means begin with clusters: 130\n",
      "CH_score: 0.504687974278, time elaps:2\n",
      "v_score: 0.779605078543\n",
      "K-means begin with clusters: 140\n",
      "CH_score: 0.537249731312, time elaps:2\n",
      "v_score: 0.781643007576\n",
      "K-means begin with clusters: 150\n",
      "CH_score: 0.545508833275, time elaps:2\n",
      "v_score: 0.788098691917\n",
      "K-means begin with clusters: 160\n",
      "CH_score: 0.550284453896, time elaps:2\n",
      "v_score: 0.793936195722\n",
      "K-means begin with clusters: 170\n",
      "CH_score: 0.542503002263, time elaps:2\n",
      "v_score: 0.80039270637\n",
      "K-means begin with clusters: 180\n",
      "CH_score: 0.545597275062, time elaps:3\n",
      "v_score: 0.802824994372\n",
      "K-means begin with clusters: 190\n",
      "CH_score: 0.526901268219, time elaps:3\n",
      "v_score: 0.805867975001\n",
      "K-means begin with clusters: 200\n",
      "CH_score: 0.539961045981, time elaps:3\n",
      "v_score: 0.812009732034\n",
      "K-means begin with clusters: 210\n",
      "CH_score: 0.546149402719, time elaps:3\n",
      "v_score: 0.817967958304\n",
      "K-means begin with clusters: 220\n",
      "CH_score: 0.518848730911, time elaps:3\n",
      "v_score: 0.823687098276\n",
      "K-means begin with clusters: 230\n",
      "CH_score: 0.523217359452, time elaps:4\n",
      "v_score: 0.82644385705\n",
      "K-means begin with clusters: 240\n",
      "CH_score: 0.543892469177, time elaps:4\n",
      "v_score: 0.82631563476\n",
      "K-means begin with clusters: 250\n",
      "CH_score: 0.540944677856, time elaps:4\n",
      "v_score: 0.827445602083\n",
      "K-means begin with clusters: 260\n",
      "CH_score: 0.520887341796, time elaps:4\n",
      "v_score: 0.834282726153\n",
      "K-means begin with clusters: 270\n",
      "CH_score: 0.512682998387, time elaps:5\n",
      "v_score: 0.839013131296\n",
      "K-means begin with clusters: 280\n",
      "CH_score: 0.527328370023, time elaps:5\n",
      "v_score: 0.839640185041\n",
      "K-means begin with clusters: 290\n",
      "CH_score: 0.488620216842, time elaps:4\n",
      "v_score: 0.843309315375\n",
      "K-means begin with clusters: 300\n",
      "CH_score: 0.504543388805, time elaps:5\n",
      "v_score: 0.850472565929\n",
      "K-means begin with clusters: 310\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=310. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.530083081262, time elaps:5\n",
      "v_score: 0.847610072176\n",
      "K-means begin with clusters: 320\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=320. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.523523352657, time elaps:6\n",
      "v_score: 0.857104775681\n",
      "K-means begin with clusters: 330\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=330. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.5519591938, time elaps:6\n",
      "v_score: 0.853462287367\n",
      "K-means begin with clusters: 340\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=340. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.527089691616, time elaps:6\n",
      "v_score: 0.861134306333\n",
      "K-means begin with clusters: 350\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=350. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.522653751127, time elaps:7\n",
      "v_score: 0.861942079749\n",
      "K-means begin with clusters: 360\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=360. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.536725738526, time elaps:7\n",
      "v_score: 0.861897065949\n",
      "K-means begin with clusters: 370\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=370. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.54965060583, time elaps:7\n",
      "v_score: 0.860715732985\n",
      "K-means begin with clusters: 380\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=380. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.492870743504, time elaps:7\n",
      "v_score: 0.864943486534\n",
      "K-means begin with clusters: 390\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=390. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.524115771633, time elaps:7\n",
      "v_score: 0.861566044191\n",
      "K-means begin with clusters: 400\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=400. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.501518096731, time elaps:8\n",
      "v_score: 0.868177434443\n",
      "K-means begin with clusters: 410\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=410. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.524450680675, time elaps:9\n",
      "v_score: 0.872260508075\n",
      "K-means begin with clusters: 420\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=420. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.53704796304, time elaps:8\n",
      "v_score: 0.869787060051\n",
      "K-means begin with clusters: 430\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=430. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.495327597395, time elaps:9\n",
      "v_score: 0.876823958313\n",
      "K-means begin with clusters: 440\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=440. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.529900885844, time elaps:9\n",
      "v_score: 0.879733528438\n",
      "K-means begin with clusters: 450\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=450. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.510391402529, time elaps:9\n",
      "v_score: 0.881234589743\n",
      "K-means begin with clusters: 460\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=460. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.538386953408, time elaps:10\n",
      "v_score: 0.879525116614\n",
      "K-means begin with clusters: 470\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=470. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.48713732115, time elaps:10\n",
      "v_score: 0.87679235309\n",
      "K-means begin with clusters: 480\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=480. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.485006127233, time elaps:10\n",
      "v_score: 0.881981442687\n",
      "K-means begin with clusters: 490\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=490. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.49857563042, time elaps:11\n",
      "v_score: 0.881933909196\n",
      "K-means begin with clusters: 500\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=500. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.506494980417, time elaps:11\n",
      "v_score: 0.881470711856\n",
      "K-means begin with clusters: 510\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=510. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.539122151248, time elaps:12\n",
      "v_score: 0.887752697902\n",
      "K-means begin with clusters: 520\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=520. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.51154193537, time elaps:12\n",
      "v_score: 0.886708630527\n",
      "K-means begin with clusters: 530\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=530. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.501597601852, time elaps:12\n",
      "v_score: 0.884916338874\n",
      "K-means begin with clusters: 540\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=540. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.50008160154, time elaps:13\n",
      "v_score: 0.887370854189\n",
      "K-means begin with clusters: 550\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=550. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.536167708619, time elaps:14\n",
      "v_score: 0.89130958625\n",
      "K-means begin with clusters: 560\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=560. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.537503081638, time elaps:14\n",
      "v_score: 0.89269824997\n",
      "K-means begin with clusters: 570\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=570. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.532079341389, time elaps:15\n",
      "v_score: 0.895151513784\n",
      "K-means begin with clusters: 580\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=580. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.501004242532, time elaps:15\n",
      "v_score: 0.894788735112\n",
      "K-means begin with clusters: 590\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=590. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.493634199972, time elaps:15\n",
      "v_score: 0.894231281944\n",
      "K-means begin with clusters: 600\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=600. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.491494544908, time elaps:15\n",
      "v_score: 0.895176561059\n",
      "K-means begin with clusters: 610\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=610. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.500181297546, time elaps:16\n",
      "v_score: 0.898431988518\n",
      "K-means begin with clusters: 620\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=620. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.497283567686, time elaps:16\n",
      "v_score: 0.901554408051\n",
      "K-means begin with clusters: 630\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=630. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.528756182783, time elaps:18\n",
      "v_score: 0.904535155111\n",
      "K-means begin with clusters: 640\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=640. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.494986112801, time elaps:17\n",
      "v_score: 0.900750605629\n",
      "K-means begin with clusters: 650\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=650. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.53710356617, time elaps:19\n",
      "v_score: 0.905256119253\n",
      "K-means begin with clusters: 660\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=660. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.532216950605, time elaps:20\n",
      "v_score: 0.904763118495\n",
      "K-means begin with clusters: 670\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=670. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.51687777413, time elaps:19\n",
      "v_score: 0.907856956045\n",
      "K-means begin with clusters: 680\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=680. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.499638984276, time elaps:20\n",
      "v_score: 0.905104638903\n",
      "K-means begin with clusters: 690\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=690. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.527739269426, time elaps:21\n",
      "v_score: 0.908952202367\n",
      "K-means begin with clusters: 700\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=700. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.512119977737, time elaps:21\n",
      "v_score: 0.903319735379\n",
      "K-means begin with clusters: 710\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=710. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.519047718102, time elaps:21\n",
      "v_score: 0.904073388092\n",
      "K-means begin with clusters: 720\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=720. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.512719598895, time elaps:22\n",
      "v_score: 0.907708248291\n",
      "K-means begin with clusters: 730\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=730. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.500502013465, time elaps:22\n",
      "v_score: 0.907464662454\n",
      "K-means begin with clusters: 740\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=740. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.492305722109, time elaps:23\n",
      "v_score: 0.910112184951\n",
      "K-means begin with clusters: 750\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=750. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.517248641552, time elaps:24\n",
      "v_score: 0.909509867447\n",
      "K-means begin with clusters: 760\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=760. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.513777317169, time elaps:24\n",
      "v_score: 0.90631947591\n",
      "K-means begin with clusters: 770\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=770. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.5078929315, time elaps:24\n",
      "v_score: 0.913068460739\n",
      "K-means begin with clusters: 780\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=780. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.503037558271, time elaps:25\n",
      "v_score: 0.911876866714\n",
      "K-means begin with clusters: 790\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=790. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.508930249549, time elaps:26\n",
      "v_score: 0.910776224739\n",
      "K-means begin with clusters: 800\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=800. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.512166983172, time elaps:26\n",
      "v_score: 0.910368700663\n",
      "K-means begin with clusters: 810\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=810. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.512094312766, time elaps:27\n",
      "v_score: 0.910403080483\n",
      "K-means begin with clusters: 820\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=820. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.505930579822, time elaps:27\n",
      "v_score: 0.915531544286\n",
      "K-means begin with clusters: 830\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=830. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.499233220791, time elaps:29\n",
      "v_score: 0.917653267583\n",
      "K-means begin with clusters: 840\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=840. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.502083412576, time elaps:29\n",
      "v_score: 0.915268807142\n",
      "K-means begin with clusters: 850\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=850. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.507653081795, time elaps:30\n",
      "v_score: 0.916930337664\n",
      "K-means begin with clusters: 860\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=860. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.503899198574, time elaps:30\n",
      "v_score: 0.914659441896\n",
      "K-means begin with clusters: 870\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=870. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CH_score: 0.512254776007, time elaps:31\n",
      "v_score: 0.914546325892\n",
      "K-means begin with clusters: 880\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wuzhong/anaconda3/envs/py27/lib/python2.7/site-packages/sklearn/cluster/k_means_.py:1381: RuntimeWarning: init_size=300 should be larger than k=880. Setting it to 3*k\n",
      "  init_size=init_size)\n"
     ]
    }
   ],
   "source": [
    "def K_cluster_analysis(K, X_train, y_train, X_val, y_val):\n",
    "    start = time.time()\n",
    "\n",
    "    print(\"K-means begin with clusters: {}\".format(K));\n",
    "\n",
    "    # K-means,在训练集上训练\n",
    "    mb_kmeans = MiniBatchKMeans(n_clusters=K)\n",
    "    mb_kmeans.fit(X_train)\n",
    "\n",
    "    # 在训练集和测试集上测试\n",
    "    # y_train_pred = mb_kmeans.fit_predict(X_train)\n",
    "    y_val_pred = mb_kmeans.predict(X_val)\n",
    "\n",
    "    # 以前两维特征打印训练数据的分类结果\n",
    "    # plt.scatter(X_train[:, 0], X_train[:, 1], c=y_pred)\n",
    "    # plt.show()\n",
    "\n",
    "    # K值的评估标准\n",
    "    # 常见的方法有轮廓系数Silhouette Coefficient和Calinski-Harabasz Index\n",
    "    # 这两个分数值越大则聚类效果越好\n",
    "    # CH_score = metrics.calinski_harabaz_score(X_train,mb_kmeans.predict(X_train))\n",
    "    CH_score = metrics.silhouette_score(X_train, mb_kmeans.predict(X_train))\n",
    "\n",
    "    # 也可以在校验集上评估K\n",
    "    v_score = metrics.v_measure_score(y_val, y_val_pred)\n",
    "\n",
    "    end = time.time()\n",
    "    print(\"CH_score: {}, time elaps:{}\".format(CH_score, int(end - start)))\n",
    "    print(\"v_score: {}\".format(v_score))\n",
    "\n",
    "    return CH_score, v_score\n",
    "\n",
    "\n",
    "# Ks = [10,20]\n",
    "Ks = np.arange(10,1000,10)\n",
    "CH_scores = []\n",
    "v_scores = []\n",
    "for K in Ks:\n",
    "    ch,v = K_cluster_analysis(K, X_train_part, y_train_part, X_val, y_val)\n",
    "    CH_scores.append(ch)\n",
    "    v_scores.append(v)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(Ks, np.array(CH_scores), 'b-')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(Ks, np.array(v_scores), 'g-')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
